<template>
  <div>
    <el-form
      ref="formBizEvluateSurvey"
      :model="formBizEvluateSurvey"
      label-width="75px"
      :size="defaultFormItemSize"
      label-position="right"
      @submit.native.prevent
    >
      <filter-box :item-width="350" @search="refreshFormBizEvluateSurvey(true)" @reset="onReset">
        <el-form-item label="问卷名称" prop="formFilter.surveyName">
          <el-input
            class="filter-item"
            v-model="formBizEvluateSurvey.formFilter.surveyName"
            :clearable="true"
            placeholder="问卷名称"
          />
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      class="page-table"
      :data="formBizEvluateSurvey.survey.impl.dataList"
      :size="defaultFormItemSize"
      @sort-change="formBizEvluateSurvey.survey.impl.onSortChange"
      @refresh="refreshFormBizEvluateSurvey(true)"
      :seq-config="{
        startIndex:
          (formBizEvluateSurvey.survey.impl.currentPage - 1) *
          formBizEvluateSurvey.survey.impl.pageSize
      }"
    >
      <el-button
        slot="operator"
        type="primary"
        icon="el-icon-plus"
        :size="defaultFormItemSize"
        @click="onAddRow()"
        >新建</el-button
      >

      <vxe-column title="序号" type="seq" width="50px" />
      <vxe-column title="问卷名称" field="surveyName" />
      <vxe-column title="题目数量" field="surveyCount" />
      <vxe-column title="发布状态">
        <template slot-scope="scope">
          <span>{{ scope.row.published ? '已发布' : '未发布' }}</span>
        </template>
      </vxe-column>
      <vxe-column title="创建时间">
        <template slot-scope="scope">
          <span>{{ formatDateByStatsType(scope.row.createTime, 'day') }}</span>
        </template>
      </vxe-column>
      <vxe-column title="操作" fixed="right" width="230px">
        <template slot-scope="scope">
          <el-button
            v-show="!scope.row.published"
            class="btn-table-edit"
            type="text"
            :size="defaultFormItemSize"
            @click="onEditRow(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            v-show="!scope.row.published"
            class="btn-table-edit"
            type="text"
            :size="defaultFormItemSize"
            @click="onDeleteRow(scope.row)"
          >
            删除
          </el-button>
          <el-button
            v-show="!scope.row.published"
            class="btn-table-edit"
            type="text"
            :size="defaultFormItemSize"
            @click="publish(scope.row)"
          >
            发布
          </el-button>
        </template>
      </vxe-column>

      <el-row slot="pagination" type="flex" justify="end" style="margin-top: 16px">
        <el-pagination
          :total="formBizEvluateSurvey.survey.impl.totalCount"
          :current-page="formBizEvluateSurvey.survey.impl.currentPage"
          :page-size="formBizEvluateSurvey.survey.impl.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, prev, pager, next, sizes"
          @current-change="formBizEvluateSurvey.survey.impl.onCurrentPageChange"
          @size-change="formBizEvluateSurvey.survey.impl.onPageSizeChange"
        >
        </el-pagination>
      </el-row>
    </table-box>
  </div>
</template>

<script>
import { DropdownWidget, TableWidget } from '@/utils/widget.js';
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins';
/* eslint-disable-next-line */
import {BizEvluateSurveyController} from '@/api';
import formsEditBizEvluateSurvey from '@/views/biz/formsEditBizEvluateSurvey/index.vue';

export default {
  name: 'formBizEvluateSurvey',
  props: {},
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data() {
    return {
      formBizEvluateSurvey: {
        formFilter: {
          surveyName: ''
        },
        formFilterCopy: {
          surveyName: ''
        },
        survey: {
          impl: new TableWidget(
            this.loadBizEvluateSurveyData,
            this.loadBizEvluateSurveyVerify,
            true,
            false,
            'createTime',
            1
          )
        },
        isInit: false
      }
    };
  },
  methods: {
    onReset() {
      this.$refs.formBizEvluateSurvey.resetFields();
      this.refreshFormBizEvluateSurvey(true);
    },
    onAddRow() {
      let newData = {};
      this.$dialog
        .show(
          '新建评估问卷',
          formsEditBizEvluateSurvey,
          {
            area: ['1200px', '792px']
          },
          {
            rowData: newData
          }
        )
        .then(res => {
          this.refreshFormBizEvluateSurvey();
        })
        .catch(() => {});
    },
    onEditRow(row) {
      this.loadRowData(row)
        .then(rowData => {
          this.$dialog
            .show(
              '编辑评估问卷',
              formsEditBizEvluateSurvey,
              {
                area: '1200px'
              },
              {
                rowData: rowData
              }
            )
            .then(res => {
              this.refreshFormBizEvluateSurvey();
            })
            .catch(e => {});
        })
        .catch(e => {
          //
        });
    },
    onDeleteRow(row) {
      const params = {
        surveyId: row.id
      };
      this.$confirm('是否删除问卷,' + row.surveyName + '？')
        .then(res => {
          return BizEvluateSurveyController.logicDelete(this, params);
        })
        .then(res => {
          if (res.success) {
            this.$message.success('删除成功');
            this.formInit();
          } else {
            this.$message.error(res.errorMessage);
          }
        })
        .catch(e => {
          console.error(e);
        });
    },
    loadRowData(row) {
      return new Promise((resolve, reject) => {
        let params = {
          id: row.id
        };

        BizEvluateSurveyController.findById(this, params)
          .then(res => {
            resolve(res.data);
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadBizEvluateSurveyData(params) {
      params.surveyFilter = {
        surveyName: this.formBizEvluateSurvey.formFilterCopy.surveyName
      };
      return new Promise((resolve, reject) => {
        BizEvluateSurveyController.page(this, params)
          .then(res => {
            resolve({
              dataList: res.data.dataList,
              totalCount: res.data.totalCount
            });
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadBizEvluateSurveyVerify() {
      this.formBizEvluateSurvey.formFilterCopy.surveyName = this.formBizEvluateSurvey.formFilter.surveyName;
      return true;
    },
    refreshFormBizEvluateSurvey(reloadData = false) {
      // 重新获取数据组件的数据
      if (reloadData) {
        this.formBizEvluateSurvey.survey.impl.refreshTable(true, 1);
      } else {
        this.formBizEvluateSurvey.survey.impl.refreshTable();
      }
      this.formBizEvluateSurvey.isInit = true;
    },
    onResume() {
      this.refreshFormBizEvluateSurvey();
    },
    initFormData() {},
    formInit() {
      this.initFormData();
      this.refreshFormBizEvluateSurvey();
    },
    publish(item) {
      return new Promise((resolve, reject) => {
        let params = {
          id: item.id
        };
        BizEvluateSurveyController.publish(this, params)
          .then(res => {
            this.$message.success('发布成功');
            this.refreshFormBizEvluateSurvey();
          })
          .catch(e => {
            this.$message.success('发布失败');
            reject(e);
          });
      });
    }
  },
  created() {
    this.formInit();
  }
};
</script>
